package smart_campus_api;

import java.io.IOException;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
//import com.google.gson.Gson;

public class HttpClientUtil2 {
    
    private final static String CONTENT_TYPE_TEXT_JSON = "text/json";
    
    public static String postRequest(String url, Map<String, Object> param) throws ClientProtocolException, IOException{
        
        CloseableHttpClient client = HttpClients.createDefault();

        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
        
        //Gson gson = new Gson();
        //String parameter = gson.toJson(param);
        String parameter = JSON.toJSONString(param);

        StringEntity se = new StringEntity(parameter);
        se.setContentType(CONTENT_TYPE_TEXT_JSON);
        httpPost.setEntity(se);

        CloseableHttpResponse response = client.execute(httpPost);
        HttpEntity entity = response.getEntity();
        String result = EntityUtils.toString(entity, "UTF-8");
        
        return result;
    }
    
public static String postRequest2(String url, Map<String, Object> paramMap) throws ClientProtocolException, IOException{
        
        CloseableHttpClient client = HttpClients.createDefault();

        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
        
        String parameter = JSON.toJSONString(paramMap);
        System.out.println("parameter"+parameter);

        StringEntity se = new StringEntity(parameter);
        se.setContentType(CONTENT_TYPE_TEXT_JSON);
        httpPost.setEntity(se);

        CloseableHttpResponse response = client.execute(httpPost);
        HttpEntity entity = response.getEntity();
        String result = EntityUtils.toString(entity, "UTF-8");
        
        return result;
    }
    
    public static String doPost(String url,String params) throws ClientProtocolException, IOException {
		String response = null;
		
		String sendUrl = url;
		
		HttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(sendUrl);
		
		 if (StringUtils.isNotBlank(params)) {
			 httpPost.setEntity(new StringEntity(params, "utf-8"));
	     }
		HttpResponse httpResponse = httpClient.execute(httpPost); 
		if (httpResponse.getStatusLine().getStatusCode() == 200) {
			// 请求和响应都成功了
			HttpEntity entity = httpResponse.getEntity();// 调用getEntity()方法获取到一个HttpEntity实例
			response = EntityUtils.toString(entity, "utf-8");// 用EntityUtils.toString()这个静态方法将HttpEntity转换成字符串,防止
																// //服务器返回的数据带有中文,所以在转换的时候将字符集指定成utf-8就可以了
		}
		return response;
	}

}

